# encoding: utf-8
"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lane.chang on '17/10/2024'
    comment: 服务流程
"""
from fastapi import APIRouter, Request, Body

from config import Config
from project.core.function import json_response
from project.lib.validators import generate_validator, MinuteStr
from project.lib.decorator import api_common, exec_duration
from project.lib.common import Global
from project.model.basic import Dates, Selector
from project.service.node import NodeService
from project.service.record import RecordService
from project.micro_service.apollo import Apollo


node_router = APIRouter(prefix='/c', tags=['cariabot.node'])


@node_router.post('/node/detail', summary='节点详情')
@api_common(doc_replace=(('{0}', Config.ENV_DOC_FIX),), dev_info=True)
@exec_duration(add_return=True)
async def get_node_detail(request: Request,
                          flow_code: str = Body(..., title='流程编号'),
                          node_code: str = Body('', title='节点编号'),
                          node_name: str = Body('', title='节点名称'),
                          date: Dates.enums(fixed=Global.count(), with_blank=True) = Body('', title='日期', description='today(今日)/tomorrow(明日)/after_tomorrow(后日)'),
                          time: generate_validator(MinuteStr, blank_enable=True) = Body('', title='时间', description='格式: %H:%M'),
                          selectors: list[Selector] = Body(None, title='流程选择信息'),
                          device=None,
                          session=None):
    """
    **desc**: 节点详情

    **url**: http(s)://{0}.udicaria.com/c/node/detail

    **method**: POST

    **params**:

        <body>:
            flow_code: 流程编号
            node_code: 节点编号
            date: 日期列表 用于筛选分组
            time: 时间  用于筛选分组
            selectors: 流程选择信息

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: {
                is_display_reducible: 是否显示可约量(康养版)
                is_display_calendar: 是否显示日历(康养版)
                meal_periods: 时段
                time_durations: 时长列表(康养版)
                time_mode: 时间模式()
            }
            exec_time: 接口后端处理时长
        }
    """
    ret_data = await NodeService.get_node_detail(session,
                                                 flow_code=flow_code,
                                                 node_code=node_code,
                                                 node_name=node_name,
                                                 device=device,
                                                 date=date,
                                                 time=time,
                                                 selectors=selectors)

    # 提交服务记录
    if ret_data and selectors and ret_data['node_type'] == 'success':
        """
        """
        await RecordService.do_flow_record_submit(session, flow_code, device, selectors)

        # 清空agent记忆
        await Apollo.init_user_memories(dev_id=device.dev_id)

    return json_response(data=ret_data)


@node_router.post('/node/next/detail', summary='下一节点详情')
@api_common(doc_replace=(('{0}', Config.ENV_DOC_FIX),), dev_info=True)
@exec_duration(add_return=True)
async def get_next_node_detail(request: Request,
                               flow_code: str = Body(..., title='流程编号'),
                               node_code: str = Body('', title='节点编号'),
                               selectors: list[Selector] = Body(None, title='流程选择信息'),
                               device=None,
                               session=None):
    """
    **desc**: 下一节点详情

    **url**: http(s)://{0}.udicaria.com/c/node/next/detail

    **method**: POST

    **params**:

        <body>:
            flow_code: 流程编号
            node_code: 节点编号
            selectors: 流程选择信息

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: {
                is_display_reducible: 是否显示可约量(康养版)
                is_display_calendar: 是否显示日历(康养版)
                meal_periods: 时段
                time_durations: 时长列表(康养版)
            }
            exec_time: 接口后端处理时长
        }
    """
    ret_data = await NodeService.get_next_node_detail(session,
                                                      flow_code=flow_code,
                                                      node_code=node_code,
                                                      device=device,
                                                      selectors=selectors)

    return json_response(data=ret_data)


